class Cache {

    static getInstance() { //单例
        if (!Cache._instance) {
            Cache._instance = new Cache();
        }
        return Cache._instance;
    }

    time = new Date().getDate();

    buffer = {};

    //清楚缓存
    clear() {
        this.buffer = {};
    }

    //检查过期
    checkTimeOut() {
        let n = new Date().getDate();
        if(n == 1 || this.time < n ){
            this.buffer = {};
            this.time = n;
        }
    }

    //使用缓存
    useCache(key, param, callBack) {
        this.checkTimeOut();

    	param = param || 'noParam';
    	param = encodeURI(JSON.stringify(param));

    	this.buffer[key] = this.buffer[key] || {};
    	if(this.buffer[key][param]) {
    		callBack(this.buffer[key][param]);
    		return true;
    	} else {
    		return false;
    	}
    }

    //保存缓存
    saveCache(key, param, data) {
    	param = param || 'noParam';
    	param = encodeURI(JSON.stringify(param));
    	
    	this.buffer[key] = this.buffer[key] || {};
    	this.buffer[key][param] = data;
    }
}

export default Cache.getInstance();